/*
Copyright (c) 2010 Neil Wood

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package envisage.Controls
{
	import envisage.Data.IRange;
	import envisage.Data.UIRangeEvent;
	import envisage.Notation;
	
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	public class NumericStepper extends Control
	{
		public function NumericStepper()
		{
			super();
			
			NoScale = true;
			
			tf = new TextField();
			tf.selectable = true;
			tf.type = TextFieldType.INPUT;
			tf.multiline = false;
			tf.wordWrap = false;
			tf.defaultTextFormat = new TextFormat("Arial", 12, 0x000000, false, false, false, null, null, TextFormatAlign.LEFT);
			addChild(tf);
			
			tf.addEventListener(FocusEvent.FOCUS_OUT, OnTextFocusOut);
			
			addEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown);
		}	
		
		private function OnTextFocusOut(e:FocusEvent):void
		{
			if( iRange == null ) return;
			var p:Number = parseFloat(tf.text);
			if( isNaN(p) ) p = iRange.GetA();
			SetValue(p);
		}
		
		private var keyCodeUp:int = 38;
		public function set KeyCodeUp(value:int):void { keyCodeUp = value; }
		
		private var keyCodeDown:int = 40;
		public function set KeyCodeDown(value:int):void { keyCodeDown = value; }
		
		private function OnKeyDown(e:KeyboardEvent):void
		{
				 if( e.keyCode == keyCodeUp )   OffsetValue( step);
			else if( e.keyCode == keyCodeDown ) OffsetValue(-step);
		}
		
		private var iRange:IRange;
		public function get DataRange():IRange { return iRange; }
		public function set DataRange(value:IRange):void
		{
			if( iRange != null )
			{
				iRange.removeEventListener(UIRangeEvent.UPDATED_A, OnUpdatedA);
				iRange.removeEventListener(UIRangeEvent.UPDATED_B, OnUpdatedB);
				iRange.removeEventListener(UIRangeEvent.UPDATED_POS, OnUpdatedPos);
			}
			
			iRange = value;
			
			if( iRange != null )
			{
				iRange.addEventListener(UIRangeEvent.UPDATED_A, OnUpdatedA);
				iRange.addEventListener(UIRangeEvent.UPDATED_B, OnUpdatedB);
				iRange.addEventListener(UIRangeEvent.UPDATED_POS, OnUpdatedPos);
			}
			
			RequireLayout();
		}
		
		private var cyclic:Boolean = false;
		public function get Cyclic():Boolean { return cyclic; }
		public function set Cyclic(value:Boolean):void { this.cyclic = value; }
		
		
		private var buttonUp:Button;
		public function get ButtonUp():Button { return buttonUp; }
		public function set ButtonUp(value:Button):void
		{
			buttonUp = value;
			buttonUp.SetAutoRepeat(true);
			buttonUp.addEventListener(EnvisageEvent.SELECTED, OnButtonSelected);
			RequireLayout();
		}
		
		private var buttonDown:Button;
		public function get ButtonDown():Button { return buttonDown; }
		public function set ButtonDown(value:Button):void
		{
			buttonDown = value;
			buttonDown.SetAutoRepeat(true);
			buttonDown.addEventListener(EnvisageEvent.SELECTED, OnButtonSelected);
			RequireLayout();
		}
		
		private function OnButtonSelected(e:EnvisageEvent):void
		{
			if( e.control == buttonUp ) OffsetValue(step);
			if( e.control == buttonDown) OffsetValue(-step);
		}
		
		private var decimalPlaces:int = 1;
		public function get DecimalPlaces():int { return decimalPlaces; }
		public function set DecimalPlaces(value:int):void { decimalPlaces = value; RequireLayout();}
		
		private var step:Number = 1;
		public function get Step():Number { return step; }
		public function set Step(value:Number):void { step = value; }
		
	//	private var stepAcc:Number = 1;
	//	public function get StepAcc():Number {return stepAcc; }
	//	public function set StepAcc(value:Number):void { stepAcc = value; }
		
		private var tf:TextField;
		
		public function set DefaultTextFormat(value:TextFormat):void { tf.defaultTextFormat = value; }
		
		public function OffsetValue(offset:Number):void
		{
			if( iRange == null ) return;
			
			SetValue(iRange.GetPos() + offset);
		}
		
		public function SetValue(value:Number):void
		{
			if( iRange == null ) return;
			
			var a:Number = iRange.GetA();
			var b:Number = iRange.GetB();
			var p:Number = iRange.GetPos();
			p = value;
			
			
			if( b > a )
			{
				if( p < a )
				{
					if( cyclic == false ) p = a;
					else p += (b-a);
				}
			
				if( p > b )
				{
					if( cyclic == false ) p = b;
					else p -= (b-a);
				}
			}
			
			else
			{
				if( p < b )
				{
					if( cyclic == false ) p = b;
					else p += (a-b);
				}
				
				if( p > a )
				{
					if( cyclic == false ) p = a;
					else p -= (a-b);
				}
			}
			
			
			iRange.SetPos(p);
		}
		
		protected override function Layout():void
		{
			if( iRange == null ) return;
			
			var p:Number = iRange.GetPos();
			
			tf.text = Notation.NumberDecimalPlaces(p, decimalPlaces);
			
			tf.width = width;
			tf.height = height;
			
			if( buttonUp != null ) {buttonUp.x = width-buttonUp.width; buttonUp.y = 0; }
			if( buttonDown != null ) { buttonDown.x = width-buttonDown.width; buttonDown.y = height-buttonDown.height; }
		}
		
		private function OnUpdatedA(e:UIRangeEvent):void   { RequireLayout(); }
		private function OnUpdatedB(e:UIRangeEvent):void   { RequireLayout(); }
		private function OnUpdatedPos(e:UIRangeEvent):void { RequireLayout(); }
	}
}
